import { HotWangzhanRes, WeekDataRes } from "@/api/types/dataAsset";
import { EChartsOption } from "echarts";
import { useEffect, useState } from "react";
import { createHotwangzhanEchartsOption, createWeekEchartsOption } from "../config/echarts";
import { getHotWangzhanData, getWeekData } from "@/api/dataAsset";

// 数据采集趋势
export function useWeekData() {
  const [weekData, setWeekData] = useState<WeekDataRes>([
      { '房产': [] },
      { '学校': [] },
      { '美团商家': [] },
      { '其他': [] }
    ]
  )

  const [weekOption, setWeekOption] = useState<EChartsOption>(createWeekEchartsOption(weekData))

  const request = async () => {
    try {
      const res = await getWeekData()

      if (res.code === 200) {
        setWeekData(res.data)
        setWeekOption(() => createWeekEchartsOption(res.data))
      }
    } catch (error) {
      console.error(error)
    }
  }

  useEffect(() => {
    request()
  }, [])

  return [ weekOption ]
}

// 热门网站采集数量
export function useHotwangzhan() {
  const [hotData, setHotData] = useState<HotWangzhanRes>([]),
   [echartsOption, setEchartsOption] = useState<EChartsOption>(createHotwangzhanEchartsOption(hotData))

  const request = async () => {
    try {
      const res = await getHotWangzhanData()

      if (res.code === 200) {
        setHotData(res.data)
        setEchartsOption(createHotwangzhanEchartsOption(res.data))
      }
    } catch (error) {
      console.error(error)
    }
  }

  useEffect(() => {
    request()
  }, [])

  return [ echartsOption ] as const
}